/**
 * 部署流程deployment.js
 * 版本号： v1.0.0
 */
$(function () {

    $("#deployment_dataList").datagrid({
        title:"流程部署信息",
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        pageSize: 10,
        pageList: [10, 20, 50, 150, 200],
        url: CONTEXT_PATH + '/deployment/deploymentList.do',
        idField: 'id',
        view: detailview,
        frozenColumns:[[
            {field:'',checkbox:true}
        ]],
        columns: [
            [
                {field: 'id', title: '部署编号', width: 60, fixed: true },
                {field: 'name', title: '部署名称', width: 180, fixed: true},
                {field: 'deploymentTime', title: '发布时间', width: 135}
            ]
        ],
        detailFormatter:function(index,row){
            return '<div style="padding:0px"><table id="subTable-' + index + '"></table></div>';
        },
        onExpandRow:function(index,row){
            $("#subTable-" + index).datagrid({
                title:"流程定义信息",
                url: CONTEXT_PATH + '/deployment/processDefinitionList.do?deploymentId=' + row.id,
                fitColumns: true,
                singleSelect:true,
                height:'auto',
                pageSize: 1,
                columns:[[
                    {field:'deploymentId', title:'部署编号', width: 60, fixed: true },
                    {field:'name', title:'定义名称', width: 180, fixed: true },
                    {field:'id', title:'定义编号', width: 140, fixed: true },
                    {field:'key', title:'定义KEY', width: 120, fixed: true},
                    {field:'version', title:'定义版本', width: 60, fixed: true},
                    {field:'resourceName', title:'定义资源名称', width: 150, fixed: true},
                    {field:'diagramResourceName', title:'定义图片名称', width: 150, fixed: true},
                    {field:'option', title:'操作', width: 120, formatter: function(value, row, index){
                        return "<span style='color: blue; font-weight: bold' onclick=\"openDialog('" + row.deploymentId + "','" + row.diagramResourceName + "')\">查看流程图</span>"
                    }}
                ]],
                onResize:function(){
                    $("#deployment_dataList").datagrid('fixDetailRowHeight', index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){ $("#deployment_dataList").datagrid('fixDetailRowHeight',index); }, 0);
                }
            });
            $('#deployment_dataList').datagrid('fixDetailRowHeight', index);
        },
        onLoadSuccess: function (data) {
            if(data.rows.length > 0){
                $("#deployment_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#deployment_Menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });








});

/**
 * 打开对话框
 * @param deploymentId
 * @param diagramResourceName
 */
function openDialog(deploymentId, diagramResourceName){

    $("#showView").dialog({
        title: "查看流程图",
        iconCls: "",
        width: window.document.body.offsetWidth,
        height: window.document.body.offsetHeight,
        cache: false,
        modal: true,
        href: CONTEXT_PATH + "/deployment/toViewImage.do?deploymentId=" + deploymentId + "&diagramResourceName=" + diagramResourceName
    }).show();


}
/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            deployment_add();
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            deployment_del();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 部署流程
 */
function deployment_add() {
    window.location.href = CONTEXT_PATH + "/deployment/toDeployment.do";
}



/**
 * 删除流程
 */
function deployment_del() {
    var row = $("#deployment_dataList").datagrid("getSelected");
    if (null == row) {
        $.messager.show({title: '操作提示', msg: "请选择删除的流程名称", timeout: 3000, showType: "slide"});
        return;
    };
    $.messager.confirm('删除提示', '删除【 ' + row.name + ' 】流程吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/deployment/delDeployment.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#deployment_dataList").datagrid("reload", {});
                }
            })
        }
    });
}






